﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Hotel.Models.Entity;
using System.Data;
using Hotel.Classes.Helper;

namespace Hotel.Models.DataAccess
{
    public class DARoomPrice
    {
        public static List<RoomPrice> GetListRoomPrice(int roomPriceID)
        {
            DataTable dt;
            List<RoomPrice> list = new List<RoomPrice>();
            using (DBManager dbManager = new DBManager("stp_Get_RoomPrice"))
            {
                dbManager.Add("p_PK_RoomPriceID", roomPriceID);
                
                dt = dbManager.GetDatatable();
            }
            foreach (DataRow dr in dt.Rows)
            {
                RoomPrice room = new RoomPrice();
                room.PkRoomPriceId = DataHelper.GetInt(dr, "PK_RoomPriceID");

                Room r = new Room();
                r.PkRoomId = DataHelper.GetInt(dr, "FK_RoomID");
                room.Room = r;

                room.DayPrice = DataHelper.GetDouble(dr, "DayPrice");
                room.DayWeek = DataHelper.GetDouble(dr, "DayWeek");
                room.DayMonth = DataHelper.GetDouble(dr, "DayMonth");
                room.Overnight = DataHelper.GetDouble(dr, "Overnight");
                room.PersonMore = DataHelper.GetInt(dr, "PersonMore");
                room.MoneyMore = DataHelper.GetDouble(dr, "MoneyMore");
                room.MoneyMoreNext = DataHelper.GetDouble(dr, "MoneyMoreNext");
                room.OneHours = DataHelper.GetDouble(dr, "OneHours");
                room.TwoHours = DataHelper.GetDouble(dr, "TwoHours");
                room.ThreeHours = DataHelper.GetDouble(dr, "ThreeHours");
                room.FourHours = DataHelper.GetDouble(dr, "FourHours");
                room.FiveHours = DataHelper.GetDouble(dr, "FiveHours");
                list.Add(room);
            }
            return list;
        }

        public static RoomPrice GetRoomPriceByRoomID(int roomID)
        {
            DataTable dt;

            using (DBManager dbManager = new DBManager("stp_Get_RoomPriceByRoomID"))
            {
                dbManager.Add("p_PK_RoomID", roomID);

                dt = dbManager.GetDatatable();
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                RoomPrice room = new RoomPrice();
                room.PkRoomPriceId = DataHelper.GetInt(dr, "PK_RoomPriceID");

                Room r = new Room();
                r.PkRoomId = DataHelper.GetInt(dr, "FK_RoomID");
                room.Room = r;

                room.DayPrice = DataHelper.GetDouble(dr, "DayPrice");
                room.DayWeek = DataHelper.GetDouble(dr, "DayWeek");
                room.DayMonth = DataHelper.GetDouble(dr, "DayMonth");
                room.Overnight = DataHelper.GetDouble(dr, "Overnight");
                room.PersonMore = DataHelper.GetInt(dr, "PersonMore");
                room.MoneyMore = DataHelper.GetDouble(dr, "MoneyMore");
                room.MoneyMoreNext = DataHelper.GetDouble(dr, "MoneyMoreNext");
                room.OneHours = DataHelper.GetDouble(dr, "OneHours");
                room.TwoHours = DataHelper.GetDouble(dr, "TwoHours");
                room.ThreeHours = DataHelper.GetDouble(dr, "ThreeHours");
                room.FourHours = DataHelper.GetDouble(dr, "FourHours");
                room.FiveHours = DataHelper.GetDouble(dr, "FiveHours");
                return room;
            }
            
            return null;
        }

        public static bool SaveRoomPrice(RoomPrice roomprice)
        {
            Room room = roomprice.Room;
            using (DBManager dbManager = new DBManager("stp_Create_RoomPrice"))
            {
                try
                {
                    dbManager.Add("p_PK_RoomPriceID", roomprice.PkRoomPriceId);
                    dbManager.Add("p_FK_RoomID", room.PkRoomId);
                    dbManager.Add("p_DayPrice", roomprice.DayPrice);
                    dbManager.Add("p_DayWeek", roomprice.DayWeek);
                    dbManager.Add("p_DayMonth", roomprice.DayMonth);
                    dbManager.Add("p_Overnight", roomprice.Overnight);
                    dbManager.Add("p_PersonMore", roomprice.PersonMore);
                    dbManager.Add("p_MoneyMore", roomprice.MoneyMore);
                    dbManager.Add("p_MoneyMoreNext", roomprice.MoneyMoreNext);
                    dbManager.Add("p_OneHours", roomprice.OneHours);
                    dbManager.Add("p_TwoHours", roomprice.TwoHours);
                    dbManager.Add("p_ThreeHours", roomprice.ThreeHours);
                    dbManager.Add("p_FourHours", roomprice.FourHours);
                    dbManager.Add("p_FiveHours", roomprice.FiveHours);
                    
                    dbManager.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
    }
}